package com.kidala.codility;

public class MaxProfit {

    public static void main(String[] args) {
        MaxProfit maxProfit = new MaxProfit();
        int[] A = new int[] { 32, 4, 4234, 423, 432, 234, 22, 1222 };
        int profit = maxProfit.maxProfit(A);
        System.out.println(profit);
    }

    int maxProfit(int[] A) {
        if (A == null || A.length < 2) {
            return 0;
        }
        int length = A.length;

        int min = A[0];
        int maxProfit = 0;
        for (int i = 0; i < length - 1; i++) {
            int first = A[i];
            int next = A[i + 1];

            if (first > -1 && first < min) {
                min = first;
            }

            int diff = next - min;
            if (diff > 0 && maxProfit < diff) {
                maxProfit = diff;
            }
        }
        return maxProfit;
    }

}
